﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
namespace LightSwitchApplication
{
    public partial class Drug
    {
        partial void Tag_Compute(ref string result)
        {
            // Set result to the desired field value
            String dose = this.Unit == "MG"? this.Dose.ToString() : ((int)(this.Dose)).ToString();
                result = this.CommercialName + "/" + dose + "-" + this.Unit + " (" + this.MedicalName + ")";
        }

        partial void Status_Compute(ref string result)
        {
            // Set result to the desired field value
            if (this.IsDiscontinued)
                result = "Discontinued";
            else if(this.Stock <= 0)
                result = "Out of stock";
            else if (this.Stock < this.MinimumStock)
                result = "Limited";
            else if (this.Stock > 0)
                result = "Sufficient";
           
        }

        partial void Drug_Created()
        {
            this.Dose = 1;
            this.RetailPrice = 0.01m;
        }

        partial void MedicalName_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            if (this.MedicalName != null && this.CommercialName != null)
            {   
                Drug record = this.DataWorkspace.ApplicationData.Drugs.Where(drugs =>
                    drugs.CommercialName == this.CommercialName &&
                    drugs.MedicalName != this.MedicalName).FirstOrDefault();
                if (record != null)
                    results.AddPropertyError("Inconsistent medical name with: " + record.Tag);
            }
        }

        partial void CommercialName_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            if (this.CommercialName != null)
            {
                if (this.DataWorkspace.ApplicationData.Drugs.Where(drugs =>
                    drugs.CommercialName == this.CommercialName && drugs.Dose == this.Dose &&
                    drugs.Id != this.Id).FirstOrDefault() != null)
                    results.AddPropertyError("Duplicated drugs: " + this.CommercialName + "/" + this.Dose);
            }
        }

        partial void CommercialName_Changed()
        {
            if (this.CommercialName != null)
                this.CommercialName = this.CommercialName.ToUpper().Trim();
        }

        partial void MedicalName_Changed()
        {
            if (this.MedicalName != null)
                this.MedicalName = this.MedicalName.ToUpper().Trim();
        }

        partial void Type_IsReadOnly(ref bool result)
        {
            result = this.SellOrders.Any(sos => sos.Prescription != null);
        }

        partial void Dose_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            if(this.Unit != null && this.Unit != "MG" && this.Dose - (int)(this.Dose) != 0)
                results.AddPropertyError("The dose must be an integer for units of tablet, capsule, drop");
        }       
    }
}
